package com.example.administrator.mybabymall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.mybabymall.R;
import com.example.administrator.mybabymall.bean.Campaign;
import com.example.administrator.mybabymall.bean.HomeCampaign;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/11/15 0015.
 */

public class HomeCatgoryAdapter extends RecyclerView.Adapter<HomeCatgoryAdapter.ViewHolder> {

    private static int LEFT = 0;
    private static int RIGHT = 1;
    private LayoutInflater layoutInflater;
    private OnCampaignClickListener onCampaignClickListener;
    public void setOnCampaignClickListener(OnCampaignClickListener campaignClickListener){
        this.onCampaignClickListener = campaignClickListener;
    }
    private List<HomeCampaign> mDatas;
    private Context mcontext;
    public HomeCatgoryAdapter(List<HomeCampaign> datas, Context context){
        mDatas = datas;
        mcontext = context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        layoutInflater = LayoutInflater.from(parent.getContext());
        if(viewType == RIGHT){
            return new ViewHolder(layoutInflater.inflate(R.layout.template_home_cardview2,parent,false));
        }

        return new ViewHolder(layoutInflater.inflate(R.layout.template_home_cardview,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        HomeCampaign homeCampaign = mDatas.get(position);
        holder.textViewTitle.setText(homeCampaign.getTitle());
//        holder.imageViewBig.setImageResource(homeCampaign.getImgBig());
//        holder.imageViewTop.setImageResource(homeCampaign.getImgSmallTop());
//        holder.imageViewBottom.setImageResource(homeCampaign.getImgSmallBottom());

        Picasso.with(mcontext).load(homeCampaign.getCpOne().getImgUrl()).into(holder.imageViewBig);
        Picasso.with(mcontext).load(homeCampaign.getCpTwo().getImgUrl()).into(holder.imageViewTop);
        Picasso.with(mcontext).load(homeCampaign.getCpThree().getImgUrl()).into(holder.imageViewBottom);

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(position % 2 == 0){
            return RIGHT;
        }else return LEFT;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView textViewTitle;
        ImageView imageViewBig;
        ImageView imageViewTop;
        ImageView imageViewBottom;
       public ViewHolder(View itemView) {
           super(itemView);

           textViewTitle = itemView.findViewById(R.id.text_title);
           imageViewBig = itemView.findViewById(R.id.imgview_big);
           imageViewTop = itemView.findViewById(R.id.imgview_small_top);
           imageViewBottom = itemView.findViewById(R.id.imgview_small_bottom);

           imageViewBig.setOnClickListener(this);
           imageViewTop.setOnClickListener(this);
           imageViewBottom.setOnClickListener(this);
       }

        @Override
        public void onClick(View v) {
           HomeCampaign homeCampaign = mDatas.get(getLayoutPosition());
           if (onCampaignClickListener!=null){
                switch (v.getId()){
                    case R.id.imgview_big:
                        onCampaignClickListener.onClick(v,homeCampaign.getCpOne());
                        break;
                    case R.id.imgview_small_top:
                        onCampaignClickListener.onClick(v,homeCampaign.getCpTwo());
                        break;
                    case R.id.imgview_small_bottom:
                        onCampaignClickListener.onClick(v,homeCampaign.getCpThree());
                        break;
                }
           }
        }
    }

   public interface OnCampaignClickListener{
        public void onClick(View viewl, Campaign campaign);
   }
}

